define(["jquery","bootstrap","bbcare","fu/app/comm/comm",
    "scprit/nutrition/app/foodLibrary/foodLibrary.servers.js",
    "scprit/nutrition/app/foodLibrary/foodLibrary.ui.js"],
    function($,bootstrap,$bb,$comm,$servers,$ui){
	var event = {};

    /**
     * 点击form表单里的确定按钮，进行添加/修改
     */
    var onSaveFoodLibiray = function(){
        var className = $(this).data("className");
        var libraryName = $(this).parents("form").find("input[name='foodLibraryName']").val();
        var remark = $(this).parents("form").find("input[name='remark']").val();
        var libraryId = $(this).attr("foodLibraryId");
        var params = {
            "foodLibraryId":libraryId, 
            "foodLibraryName":libraryName, 
            "remark":remark,
        };
        $servers.ajaxFoodlibraryModify(params,function(data){
            $("." + className).modal("hide");
            foodLibraryList();
        });
    }

    /**
     * 点击新建食品库，将form表单渲染出来
     */
    var onAddFoodLibiray = function(){
        var className = "addFoodLibraryModal";
        $comm.modal({
            "className":className,
            "tit":"添加食品库"
        },function(){
            $ui.addFoodLibirayFormInit({
                "dom":$("." + className + " .modal-body")
            });
            $("." + className).modal("show");
            $("#saveFoodLibirayBtn").unbind("click");
            $("#saveFoodLibirayBtn").data({"className":className});
            $("#saveFoodLibirayBtn").click(onSaveFoodLibiray);
        });
    }

    /**
     * 修改食品库名称
     */
    var onSetFoodLibrary = function(){
        var data = $(this).data("data");
        var foodLibraryId = $(this).parents(".food-box").attr("foodLibraryId");
        var foodLibraryData = $servers.getFoodlibraryInfo(data,foodLibraryId);
        var className = "setFoodLibraryModal";
        $comm.modal({
            "className":className,
            "tit":"修改食品库名称"
        },function(){
            $ui.addFoodLibirayFormInit({
                "dom":$("." + className + " .modal-body"),
                "data":foodLibraryData
            });
            $("." + className).modal("show");
            $("#saveFoodLibirayBtn").unbind("click");
            $("#saveFoodLibirayBtn").data({"className":className});
            $("#saveFoodLibirayBtn").click(onSaveFoodLibiray);
        });
    }

    /**
     * 跳转到内部页面
     */
    var onEditFoodLibrary = function(){
        var foodLibraryId = $(this).parent(".food-box").attr("foodLibraryId");
        $bb.openPage("editFood",foodLibraryId);
    }

    /**
     * 查询食品库成功后的回调
     */
    var ajaxFoodlibraryListCallBack = function(data){
        $ui.foodLibirayListInit({
            "id":"foodLibirayList",
            "list":data
        });
        $("#foodLibirayList .food-box").mouseenter(function(){
            $(this).find(".food-name i").removeClass("fa-folder-o").addClass("fa-folder-open-o");
            $(this).find(".food-operation").slideDown();
        }).mouseleave(function(){
            $(this).find(".food-name i").removeClass("fa-folder-open-o").addClass("fa-folder-o");
            $(this).find(".food-operation").slideUp();
        });
        // 跳转到食品库详情页
        $("#foodLibirayList .food-box .food-info").unbind("click");
        $("#foodLibirayList .food-box .food-info").click(onEditFoodLibrary);
        // 添加食品库
        $("#addFoodLibiray").unbind("click");
        $("#addFoodLibiray").click(onAddFoodLibiray);
        // 修改食品库名称
        $("#foodLibirayList .food-box .setFoodLibrary").unbind("click");
        $("#foodLibirayList .food-box .setFoodLibrary").data({"data":data});
        $("#foodLibirayList .food-box .setFoodLibrary").click(onSetFoodLibrary);
    }

    /**
     * 查询食品库
     */
    var foodLibraryList = function(){
        $servers.ajaxFoodlibraryList(ajaxFoodlibraryListCallBack);
    }
	event.foodLibraryList = function(){
        foodLibraryList();
    }

	return event;
});